package com.edu.controller;


import com.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.entity.Teacher;
import com.edu.entity.vo.TeacherQuery;
import com.edu.exceptionhandler.MyException;
import com.edu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-09
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class TeacherController {

	@Autowired
	public TeacherService teacherService;

	//查询所有
	@GetMapping("/findAll")
	public R findAll(){
		List<Teacher> list = teacherService.list(null);
		return R.ok().data("items",list);
	}

	//删除
	@DeleteMapping("/{id}")
	public R removeTeacher(@PathVariable String id){
		boolean flag = teacherService.removeById(id);
		if (flag){
			return R.ok();
		}else {
			return R.error();
		}
	}

	//分页查询
	@GetMapping("/pageTeacher/{current}/{limit}")
	public R pageListTeacher(@PathVariable long current, @PathVariable long limit){

		Page<Teacher> page = new Page<>(current,limit);

		try {
//			int i = 10/0;
		} catch (Exception e) {
			throw new MyException(404,"执行了自定义异常");
		}

		teacherService.page(page,null);
		long total = page.getTotal();
		List<Teacher> records = page.getRecords();

//		HashMap map = new HashMap();
//		map.put("total",total);
//		map.put("row",records);
//		return R.ok().data(map);


		return R.ok().data("total",total).data("rows",records);
	}

	//条件查询带分页
	@PostMapping("/pageTeacherCondition/{current}/{limit}")
	public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
								  @RequestBody(required = false) TeacherQuery teacherQuery){
		Page<Teacher> page = new Page<>(current, limit);

		QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

		String name = teacherQuery.getName();
		Integer level = teacherQuery.getLevel();
		String begin = teacherQuery.getBegin();
		String end = teacherQuery.getEnd();

		if (!StringUtils.isEmpty(name)){  // 这里，草，忘记加  ！，玛德！！！！！
			wrapper.like("name",name);
		}
		if (!StringUtils.isEmpty(level)){
			wrapper.eq("level",level);
		}
		if (!StringUtils.isEmpty(begin)){
			wrapper.ge("gmt_create",begin);
		}
		if (!StringUtils.isEmpty(end)){
			wrapper.le("gmt_modified",end);
		}

		//
		wrapper.orderByDesc("gmt_create");
		//调用方法实现查询分页
		teacherService.page(page,wrapper);

		long total = page.getTotal();
//		System.out.println(total);
		List<Teacher> records = page.getRecords();
		return R.ok().data("total",total).data("rows",records);
	}

	//添加
	@PostMapping("/addTeacher")
	public R addTeacher(@RequestBody Teacher teacher){
		boolean b = teacherService.save(teacher);
		return b==true ? R.ok():R.error();
	}


	@GetMapping("/getTeacher/{id}")
	public R getTeacher(@PathVariable String id){
		Teacher teacher = teacherService.getById(id);
		return R.ok().data("teacher",teacher);

	}

	@PostMapping("/updateTeacher")
	public R updateTeacher(@RequestBody Teacher teacher){
		boolean b = teacherService.updateById(teacher);
		return b==true ? R.ok():R.error();
	}
}

